<template>
  <InlineLayout title-name="社保接受联系函">
    <template #header>
      <my-form ref="ruleForm" v-model:modelValue="dataForm" :model="dataForm" :items-datas="itemsDatas">
        <my-button type="reset" title="重置" @click="resetQuery" />
        <my-button type="search" title="查询" :loading="loading" @click="handleQuery" />
      </my-form>
    </template>

    <template #titleTools>
      <my-button type="add" title="新增" @click="handleAdd" />
    </template>

    <my-table :columns="columns" :table-data="tableData">
      <template #operation="scope">
        <my-button type="text" @click="handleDetail(scope.row)">详情</my-button>
        <my-button type="text" @click="pdfView(scope.row)">预览</my-button>
        <my-button type="text" @click="handleUpdate(scope.row)">修改</my-button>
      </template>
    </my-table>
    <template #footer>
      <pagination v-model:page="dataForm.pageNum" v-model:limit="dataForm.pageSize" :total="total" @pagination="getList" />
    </template>
  </InlineLayout>
  <addOrEdit v-model="visible" :dialog-title="title" :current-row="currentRow" @handle-query="handleQuery" />
  <printDialog v-model="pdfVisible" :current-row="currentRow" />
</template>

<script setup name="Contact" lang="ts">
import { listContact } from '@/api/yuanshidian/contact';
import { ContactVO, ContactQuery, ContactForm } from '@/api/yuanshidian/contact/types';
import addOrEdit from './components/addOrEdit';
import printDialog from './components/printDialog';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const contactList = ref<ContactVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const total = ref(0);

const ruleForm = ref<ElFormInstance>();
const contactFormRef = ref<ElFormInstance>();

const state = reactive({
  visible: false,
  pdfVisible: false,
  title: '',
  dataForm: {
    pageNum: 1,
    pageSize: 10
  },
  currentRow: {},
  itemsDatas: [
    { label: '姓名', prop: 'name', type: 'input' },
    { label: '证件号码', prop: 'identificationCard', type: 'input' },
    { label: '转入单位', prop: 'zrdw', type: 'input' }
  ],

  tableData: [{}],
  columns: [
    { label: '序号', type: 'index', width: '55', prop: 'index' },
    { label: '转出单位', prop: 'zcdw', align: 'center' },
    { label: '个人编号', prop: 'grbh', align: 'center' },
    { label: '身份证号码', prop: 'identificationCard', align: 'center' },
    { label: '姓名', prop: 'name', align: 'center' },
    { label: '转入统筹区编码', prop: 'zrtcqbm', align: 'center' },
    { label: '转入单位', prop: 'zrdw', align: 'center' },
    { label: '经办人', prop: 'jbrName', align: 'center' },
    { label: '经办时间', prop: 'jbDate', align: 'center' },
    { label: '操作', type: 'operation', minWidth: '180px' }
  ]
});
const { itemsDatas, tableData, columns, dataForm, isShow, currentRow, visible, title, pdfVisible } = toRefs(state);

/** 查询社保接收联络函列表 */
const getList = async () => {
  loading.value = true;
  const res = await listContact(dataForm.value);
  tableData.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 表单重置 */
const reset = () => {
  contactFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  dataForm.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  ruleForm.value?.resetFields();
  handleQuery();
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  currentRow.value.id = '';
  currentRow.value.type = 'add';
  title.value = '添加社保接收联络函';
  visible.value = true;
};

/** 查看详情 */
const handleDetail = (row) => {
  currentRow.value.id = row.id;
  currentRow.value.type = 'detail';
  title.value = '查看社保接收联络函';
  visible.value = true;
};
/** 修改按钮操作 */
const handleUpdate = async (row?: ContactVO) => {
  reset();
  currentRow.value.id = row?.id;
  currentRow.value.type = 'edit';
  visible.value = true;
  title.value = '修改社保接收联络函';
};
const pdfView = (row) => {
  currentRow.value.id = row.id;
  pdfVisible.value = true;
};
onMounted(() => {
  getList();
});
</script>
